/*
 *  ChangeSettings.c
 *  AutoEllips
 *
 *  Created by Simon Martin on 16/05/2010.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

#include "ChangeSettings.h"
// functionality to change settings like substrate refractive index etc
#define WIDTH 40
#define HEIGHT 20 

int startx = 0;
int starty = 0;



void changeSettings(WINDOW *mainwnd, ellipspar_t *ellipsparams)

{
	WINDOW *menu_win2;
	int highlight = 1;
	int choice = 0;
	int c;
	
	initscr();
	
	clear();
	noecho();
	cbreak();	/* Line buffering disabled. pass on everything */
	startx = (80 - WIDTH) / 2;
	starty = (24 - HEIGHT) / 2;
	
	menu_win2 = newwin(HEIGHT, WIDTH, starty, startx);
	keypad(menu_win2, TRUE);
	mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice. Press x to return to the main menu.");
	
	refresh();
	//mvprintw(0, 0, ellipsparams->ellipspar_names[0]);
	print_param_menu(menu_win2, highlight,ellipsparams);
	while(1)
	{	c = wgetch(menu_win2);
		switch(c)
		{	case 65:
				if(highlight == 1)
					highlight = ellipsparams->nvalues;
				else
					--highlight;
				break;
			case 66:
				if(highlight == ellipsparams->nvalues)
					highlight = 1;
				else 
					++highlight;
				break;
			case 10:
			{
				choice = highlight;
				char* valueString;
				valueString=(char *)malloc(50*sizeof(char));
				
				
				
				mvwprintw(mainwnd,2, 0, "Enter new value for %s and press enter.",ellipsparams->ellipspar_names[highlight-1]);
				
				refresh();
				echo();
				scanw("%s",valueString);
				ellipsparams->ellipspar_values[highlight-1]=atof(valueString);
			}
				break;
			case 120:
				choice=-1;
				break;
			case 88:
				choice=-1;
				break;

				refresh();
				break;
		}
		print_param_menu(menu_win2, highlight,ellipsparams);
		if (choice<0) {
				return;
			}
		
		}

		
	}	


void print_param_menu(WINDOW *menu_win2, int highlight,ellipspar_t *ellipsparams)
{
	int x, y, i;	
	
	x = 2;
	y = 2;
	box(menu_win2, 0, 0);
	for(i = 0; i < ellipsparams->nvalues; ++i)
	{	if(highlight == i + 1) /* Highlight the present choice */
	{	wattron(menu_win2, A_REVERSE); 
		mvwprintw(menu_win2, y, x, "%s %f", ellipsparams->ellipspar_names[i],ellipsparams->ellipspar_values[i]);
		wattroff(menu_win2, A_REVERSE);
	}
	else
		mvwprintw(menu_win2, y, x, "%s %f", ellipsparams->ellipspar_names[i],ellipsparams->ellipspar_values[i]);
		++y;
	}
	wrefresh(menu_win2);
}

void getNewValue(WINDOW *mainwnd, int highlight,ellipspar_t *ellipsparams)
// change the selected value
{
	char* valueString;
	valueString=(char *)malloc(50*sizeof(char));
	
	
	mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice. Press x to return to the last menu.");
	refresh();
	
	
	mvprintw(4, 0, "Enter new value for %s and press enter.",ellipsparams->ellipspar_names[highlight-1]);
	
	refresh();
	echo();
	scanw("%s",valueString);
	ellipsparams->ellipspar_values[highlight-1]=atof(valueString);	  
	return;
}
